package com.caffedev.ottollotto;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class OttollottoStats extends Activity{

//	private MobileAdView adView = null;
	
	public static final int ID=0;
	public static final int INDEX_GRWNO=1;
	public static final int INDEX_GRWDATE=2;
	public static final int INDEX_FIRSTBALL=3;
	public static final int INDEX_SECONDBALL=4;
	public static final int INDEX_THIRDBALL=5;
	public static final int INDEX_FOURTHBALL=6;
	public static final int INDEX_FIFTHBALL=7;
	public static final int INDEX_SIXTHBALL=8;
	public static final int INDEX_BONUSBALL=9;
	
	private final static String TAG = "OttollottoStats";
	private Button byNum;
	private Button bySec5;
	private Button bySec10;
	private Button byColor;
	private Button byOdd;
	//private Button byRecent;
	private Button reqPattern;
	private Spinner spin_from;
	private Spinner spin_to;
	
	private ArrayAdapter<String> adspin_from;
	private ArrayAdapter<String> adspin_to;
	private List<String> lines;
	
	
	private Cursor tCursor;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.stats);
		
		//initAdam();
		
		
		byNum = (Button)findViewById(R.id.byNumber);
		bySec5 = (Button)findViewById(R.id.bySec5);
		bySec10 = (Button)findViewById(R.id.bySec10);
		byColor = (Button)findViewById(R.id.byColor);
		byOdd = (Button)findViewById(R.id.byOdd);
		//byRecent = (Button)findViewById(R.id.byRecent);
		reqPattern = (Button)findViewById(R.id.reqPattern);
		spin_from = (Spinner)findViewById(R.id.spinner1);
		spin_to = (Spinner)findViewById(R.id.spinner2);

		tCursor = OttollottoMain.mCursor;
		tCursor.moveToFirst();
	
		lines = new ArrayList<String>();
		for(int i = Integer.valueOf(tCursor.getString(INDEX_GRWNO)); i>=1 ; i--){
			lines.add(i+getString(R.string.round));
		}
		
		adspin_to = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,lines);
		adspin_to.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin_to.setAdapter(adspin_to);

		
		adspin_from = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,lines);
		adspin_from.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin_from.setAdapter(adspin_from);
		spin_from.setSelection(99);
		
		byNum.setOnClickListener(listener);
		bySec5.setOnClickListener(listener);
		bySec10.setOnClickListener(listener);
		byColor.setOnClickListener(listener);
		byOdd.setOnClickListener(listener);
		//byRecent.setOnClickListener(listener);
		reqPattern.setOnClickListener(listener);
	}
	View.OnClickListener listener = new View.OnClickListener() {
		
		
		
		@Override
		public void onClick(View v) {
			
			Intent intent = new Intent(OttollottoStats.this, OttollottoStatsView.class);
			intent.putExtra("from", spin_from.getSelectedItem().toString());
			intent.putExtra("to", spin_to.getSelectedItem().toString());
			
			switch(v.getId()){
			case R.id.byNumber:
				intent.putExtra("mode", getString(R.string.bynumber));
				startActivity(intent);
				break;
			case R.id.bySec5:
				intent.putExtra("mode", getString(R.string.bysection5));
				startActivity(intent);
				break;
			case R.id.bySec10:
				intent.putExtra("mode", getString(R.string.bysection10));
				startActivity(intent);
				break;
			case R.id.byColor:
				intent.putExtra("mode", getString(R.string.bycolor));
				startActivity(intent);
				break;
			case R.id.byOdd:
				intent.putExtra("mode", getString(R.string.byodd));
				startActivity(intent);
				break;
			/*case R.id.byRecent:
				intent.putExtra("mode", getString(R.string.byrecent));
				startActivity(intent);
				break;*/
			case R.id.reqPattern:
				String[] mailto = {"caffedev@gmail.com"};
				
				Intent i = new Intent(Intent.ACTION_SEND);
				i.setType("plain/text");
				i.putExtra(Intent.EXTRA_EMAIL, mailto);
				i.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.requestPattern));
				//i.putExtra(Intent.EXTRA_TEXT, getString(R.string.text));
				
				try{
				    startActivity(Intent.createChooser(i, "Send mail..."));
				}catch(android.content.ActivityNotFoundException ex){
				    Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
				}
				/*intent.putExtra("mode", getString(R.string.byrecent));
				startActivity(intent);*/
				break;
			}
			
		}
	};
/*	private void initAdam(){
		//AdConfig.setClientId("TestClientId");
				AdConfig.setClientId("17f2Z0QT13409633393");
				adView = (MobileAdView)findViewById(R.id.adview);
				adView.setAdListener(this);
				adView.setVisibility(View.VISIBLE);
	}*/

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	/*	if(adView != null){
			adView.destroy();
			adView=null;
		}*/
	}
	
}
